package nxu.it.controller;

import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import nxu.it.entity.Category;
import nxu.it.entity.Post;
import nxu.it.service.PostService;

import java.util.List;

/**
 * @author yangliu
 * @project jfinal-app-1
 * @sid 12020243892
 * @date 2022/12/22 - 12:45
 */
@Path(value = "/post", viewPath = "/post")
public class PostController extends Controller {
    PostService service = new PostService();

    public void category() {
        //        分页功能实现
        Integer pageNum = getInt("pageNum");
        if (pageNum == null) {
            pageNum = 1;
        } else {
            set("pageNum", pageNum);
        }
        List list = service.selectCategory(pageNum);
        set("list", list);
        renderFreeMarker("category.ftl");

    }

    public void update_category() {
        Integer cid = getInt("cid");
        Category category = service.selectCategoryByCid(cid);
        set("category", category);
        renderFreeMarker("update_category.ftl");
    }

    public void doUpdateCategory() {
        Category category = getBean(Category.class);
        System.out.println(category);
        if (service.updateCategory(category)){
            renderText("修改成功！");
        }else{
            renderText("修改失败！");
        }
    }


    public void update_post() {
        Integer pid = getInt("pid");
        Post post = service.selectPostById(pid);
        set("post", post);
        renderFreeMarker("update_post.ftl");
    }

    public void doUpdatePost() {
        Post post = getBean(Post.class);
        System.out.println(post);
        if (service.updatePost(post)){
            renderText("修改成功！");
        }else{
            renderText("修改失败！");
        }
    }

    public void doDeleteCategory() {
        Integer cid = getInt("cid");
        if (service.deleteCategory(cid)){
            renderText("删除成功！");
        }else{
            renderText("删除失败！");
        }
    }

    public void doDeleteComment() {
        Integer cid = getInt("cid");
        if (service.deleteComment(cid)){
            renderText("删除成功！");
        }else{
            renderText("删除失败！");
        }
    }
    public void doDeletePost() {
        Integer pid = getInt("pid");
        if (service.deletePost(pid)){
            renderText("删除成功！");
        }else{
            renderText("删除失败！");
        }
    }


    public void post() {
        Integer pid = getInt("pid");
        //        分页功能的实现
        Integer pageNum = getInt("pageNum");

        if (pageNum == null) {
            pageNum = 1;
        } else {
            set("pageNum", pageNum);
        }
        //  查询对应文章
        if (pid != null) {
            set("post", service.selectPostById(pid));
            set("comments", service.selectCommentByPid(pid, pageNum));
            set("pid", pid);
        }
        renderFreeMarker("post.ftl");
    }

    public void post_list() {
        Integer cid = getInt("cid");
        //        分页功能的实现
        Integer pageNum = getInt("pageNum");
        if (pageNum == null) {
            pageNum = 1;
        } else {
            set("pageNum", pageNum);
        }
        //  查询对应文章
        if (cid != null) {
            set("postList", service.selectPostsByByCategoryId(cid, pageNum));
            set("cid", cid);
        }
        renderFreeMarker("post_list.ftl");
    }


}
